home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_321 / ifs / source / ifsout.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  23KB  |  716 lines

  1. /*
  2. *
  3. *     IFSout.c - Iterated Function System
  4. *                uses IFS to create a IFS-Construction Image
  5. *     Released to the Public Domain - 1989 The Software Glen Company
  6. *    
  7. *
  8. */
  9.  
  10. #include "standard.h"
  11. #include "ifs.h"
  12. #include "ifsout.h"
  13. #include "safeclose.h"
  14.  
  15. long   timeint;
  16. short  gadcount;
  17. float  x=0., y=0.;            /* the (real) x y before translation*/
  18. UWORD  colortab[32]; 
  19. int    currentfun = 0;
  20. int    numoffun = 1;          /* startout with two functions=numfun+1 */
  21. char   fname[MAXFNLEN];
  22. char   fn[MAXFNLEN/2];
  23. char   fdir[MAXFNLEN/2] = ":coors";
  24. short   vectorsw = FALSE, 
  25.         displaynumsw = TRUE, 
  26.         pausesw = FALSE,
  27.         blackbacksw = TRUE,
  28.         adjopen = FALSE;
  29.  
  30. USHORT mx, my;                /* mouse location */
  31.  
  32. /*****************
  33. ** Main Program **
  34. *****************/
  35.  
  36. int main()
  37. {
  38.   ULONG  class;
  39.   UWORD  tempcolor;
  40.   USHORT MenuNum, ItemNum, code;
  41.   struct MenuItem *Item;   
  42.   APTR   aAddress;
  43.        
  44.   short  corner=0, adjdown = FALSE, boxmoved = FALSE;
  45.   
  46.   void   graphit();
  47.   void   gadgetmessage();
  48.   void   initialit();
  49.   void   clearscreen();
  50.   void   setcolor();
  51.   void   setupgad();
  52.   void   closeout();
  53.   void   drawvectors();
  54.   void   autoadj();
  55.   int    closeby();
  56.   void   adjust();
  57.   void   displaynums();
  58.   void   getifsfun();
  59.   void   checkswitches();
  60.  
  61.   void   putifsfun();
  62.  
  63. /* default to small flower */
  64.   float funs[FUNLIMIT][6] = { {0.20, 1.00, -0.65, 0.82, 1.11, 0.00},
  65.                               {0.40, 0.00, 0.30, -0.32, 0.32, 0.00}},
  66.         percent[FUNLIMIT] = { 0.66, 1.00}, 
  67.         xyscale = 0.11;                       /* could be .25;     */ 
  68.  
  69.   int   xoff = WIDTH/2 - 24, 
  70.         yoff = HEIGHT/2 + 28,
  71.         i, j;
  72.  
  73.   initialit(funs,percent,&xyscale,&xoff,&yoff,¤tfun);
  74.    
  75.   FOREVER {
  76.   
  77.    if (pausesw) Wait((1 << w->UserPort->mp_SigBit) | 
  78.              (1 << adjwin->UserPort->mp_SigBit));
  79.     if ((message = (struct IntuiMessage *)GetMsg(w->UserPort))) {
  80.       class = message->Class;
  81.       code  = message->Code;
  82.       mx    = message->MouseX;
  83.       my    = message->MouseY;
  84.  
  85.       aAddress = message->IAddress;
  86.          
  87.       ReplyMsg(message);       /* Can't reply until done using it!   */
  88.       switch (class) {
  89.         case  CLOSEWINDOW:   /* Exit the program */
  90.           if (adjopen) 
  91.             CloseWindowSafely(adjwin);
  92.           closeout();
  93.           exit(0);
  94.           break;
  95.         case MOUSEBUTTONS:
  96.           if (code == SELECTDOWN) {
  97.             if (vectorsw) {
  98.               if (corner=closeby (funs, ¤tfun, numoffun, mx, my) )
  99.                 adjdown = TRUE;
  100.               else 
  101.                 if (numoffun > 0) currentfun = (currentfun + 1) % 
  102.                                                (numoffun   + 1);
  103.             }
  104.             else setcolor();
  105.           }
  106.           else 
  107.             if (code == SELECTUP) {
  108.               adjdown = FALSE;
  109.           if (boxmoved) {
  110.                 clearscreen();
  111.                 boxmoved = FALSE;
  112.                 for (j=0;j<10;j++)           /* settle in on function */
  113.                     graphit(funs,percent, xyscale, xoff, yoff, FALSE); 
  114.                 }
  115.               checkswitches(rp,funs,percent,currentfun,
  116.                            numoffun,vectorsw,displaynumsw);
  117.         }
  118.           setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  119.                     xoff, yoff, currentfun, numoffun, stxt, adjopen);
  120.           break;               
  121.         case INTUITICKS:
  122.           if (adjdown) {
  123.             boxmoved = TRUE;
  124.             adjust(corner, rp, funs, currentfun, mx, my);
  125.           }
  126.           break;
  127.         case MENUPICK:
  128.           while (code != MENUNULL) {
  129.             Item = (struct MenuItem *) ItemAddress(&Menu[0], code);
  130.             MenuNum = MENUNUM( code ); 
  131.             ItemNum = ITEMNUM( code );
  132.             switch( MenuNum) {
  133.               case 0:                  /* Project Menu  */
  134.                 switch( ItemNum ) {
  135.                   case 0:              /* About Menu    */
  136.                     AutoRequest(w, &ReqText1, NULL,
  137.                       &OKIText, 10, 20, 310, 180);
  138.                     break;
  139.                   case 1:              /* Save Current  */
  140.                     if (get_fname(w,screen,"Save File",fn,fdir) 
  141.                       != NULL) {
  142.                       fname[0] = '\0';
  143.                       strcat(fname,fdir);
  144.  
  145. /* need to check if we need a / or not */
  146.                       if (fdir[strlen(fdir) - 1] != ':')
  147.                         strcat(fname,"/");
  148.  
  149.                       strcat(fname,fn);
  150.                       putifsfun(fname,numoffun,funs,percent,
  151.                                 colortab,xoff,yoff,xyscale);
  152.                       }
  153.                     checkswitches(rp,funs,percent,currentfun,
  154.                                   numoffun,vectorsw,displaynumsw);
  155.                     break;
  156.                   case 2:              /* Get IFS       */
  157.                     if (get_fname(w,screen,"Get File",fn,fdir) != NULL) {
  158.                       fname[0] = '\0';
  159.                       strcat(fname,fdir);
  160.  
  161. /* need to check if we need a / or not */
  162.                       if (fdir[strlen(fdir) - 1] != ':')
  163.                         strcat(fname,"/");
  164.                       strcat(fname,fn);
  165.                       getifsfun(fname,&numoffun,funs, percent,
  166.                                 colortab,&xoff,&yoff,&xyscale);
  167.                               
  168.                       currentfun = 0;
  169.                       PInfos[0].HorizBody = FFFF/(numoffun+1);
  170.                       setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  171.                               xoff, yoff, currentfun, numoffun, stxt, adjopen);
  172.                       for (j=0;j<10;j++)           /* settle in on function */
  173.                         graphit(funs, percent, xyscale, xoff, yoff, FALSE); 
  174.                       clearscreen();
  175.                       checkswitches(rp,funs,percent,currentfun,
  176.                                     numoffun,vectorsw,displaynumsw);
  177.                     }
  178.                     break;                
  179.                   case 3:              /* To WB         */
  180.                     ScreenToBack(screen);
  181.                     break;
  182.                   case 4:              /* quit          */
  183.                     if (adjopen) 
  184.                       CloseWindowSafely(adjwin);
  185.                     closeout();
  186.                     exit(0);
  187.                     break;
  188.                 }
  189.                 break;
  190.               case 1:                  /* Function Menu */
  191.                 switch( ItemNum ) {
  192.                   case 0:              /* Zoom to Fit   */
  193.                     x=0.; y=0.;            
  194.                     autoadj(funs, percent, &xyscale, &xoff, &yoff, ¤tfun);
  195.                     clearscreen();
  196.                     checkswitches(rp,funs,percent,currentfun,
  197.                                   numoffun,vectorsw,displaynumsw);
  198.                     break;
  199.                   case 1:              /* Zoom out x 2   */
  200.                     xyscale = xyscale / 2;
  201.                     clearscreen();
  202.                     checkswitches(rp,funs,percent,currentfun,
  203.                                   numoffun,vectorsw,displaynumsw);
  204.                     setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  205.                             xoff, yoff, currentfun, numoffun, stxt, adjopen);
  206.                     break;
  207.                   case 2:         /* Open adjust window in our new screen */
  208.                     if (!adjopen) {
  209.                       nadjwin.Screen = screen; 
  210.                       adjwin = (struct Window *)OpenWindow(&nadjwin);
  211.                       if (adjwin == NULL) {
  212.                         closeout();
  213.                         exit(6);
  214.                       }
  215.                       adjopen = TRUE;
  216.                       setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  217.                               xoff, yoff,currentfun, numoffun, stxt, adjopen);
  218.                     }
  219.                     else {
  220.                       WindowToFront(adjwin);
  221.                     }
  222.                     break;
  223.                   case 3:              /* Another Function */
  224.                     if (numoffun < FUNLIMIT-1) {
  225.                       numoffun++;
  226.                       currentfun = numoffun;   
  227.                       /* reset the new function */
  228.                       if(displaynumsw) 
  229.                         clearscreen();
  230.                       funs[currentfun][0] = 1.;
  231.                       funs[currentfun][1] = 0.;
  232.                       funs[currentfun][2] = 0.;
  233.                       funs[currentfun][3] = 1.;
  234.                       funs[currentfun][4] = 0.;
  235.                       funs[currentfun][5] = 0.;
  236.                       adjpercent(numoffun, percent);
  237.                       setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  238.                               xoff, yoff,  currentfun, numoffun, stxt, adjopen);
  239.                       checkswitches(rp,funs,percent,currentfun,
  240.                                     numoffun,vectorsw,displaynumsw);
  241.                     }
  242.                     break; 
  243.                   case 4:               /* Erase Current */
  244.                     if(numoffun > 1) {
  245.                       for (i=currentfun;i<numoffun;i++) 
  246.                         for (j=0;j<6;j++) 
  247.                          funs[i][j] = funs[i+1][j];
  248.                       funs[numoffun][0] = 1.;
  249.                       funs[numoffun][1] = 0.;
  250.                       funs[numoffun][2] = 0.;
  251.                       funs[numoffun][3] = 1.;
  252.                       funs[numoffun][4] = 0.;
  253.                       funs[numoffun][5] = 0.;
  254.                       percent[numoffun] = 0.;
  255.                       if (currentfun == numoffun) currentfun--;
  256.                       numoffun--;
  257.                       setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  258.                               xoff, yoff,  currentfun, numoffun, stxt, adjopen);
  259.                       clearscreen();
  260.                       checkswitches(rp,funs,percent,currentfun,
  261.                                     numoffun,vectorsw,displaynumsw);
  262.                     }
  263.                     break;
  264.                   case 5:               /* Erase All     */     
  265.                     numoffun = 1;       /* Leave two     */
  266.                     currentfun = 0;
  267.                     for (i=0;i<FUNLIMIT;i++)  {
  268.                       funs[i][0] = 1.;
  269.                       funs[i][1] = 0.;
  270.                       funs[i][2] = 0.;
  271.                       funs[i][3] = 1.;
  272.                       funs[i][4] = 0.;
  273.                       funs[i][5] = 0.;
  274.                       percent[i] = 0.;
  275.                     }
  276.                     percent[0]=0.5;           /* for the two left */
  277.                     percent[1]=1.;
  278.                     setpots(adjwin, gads, PInfos, funs, percent, xyscale, 
  279.                             xoff, yoff,  currentfun, numoffun, stxt, adjopen);
  280.                     clearscreen();
  281.                     checkswitches(rp,funs,percent,currentfun,
  282.                                   numoffun,vectorsw,displaynumsw);
  283.                     break;
  284.                 }   /* end of inner switch */
  285.  
  286.               break;
  287.             case 2:            /*  Switches     */
  288.               switch( ItemNum ) {
  289.                 case 0:         /*  Toggle Boxes */
  290.                   if (vectorsw) {
  291.                     vectorsw = FALSE;
  292.                     clearscreen();
  293.                   }
  294.                   else {
  295.                     vectorsw = TRUE;
  296.                   }
  297.                   checkswitches(rp,funs,percent,currentfun,
  298.                                 numoffun,vectorsw,displaynumsw);
  299.                   break;
  300.  
  301.                 case 1:        /* Toggle Numbers */
  302.                   if (displaynumsw) {
  303.                     displaynumsw = FALSE;
  304.                     clearscreen();
  305.                     if (vectorsw) 
  306.                       drawvectors(rp, funs, numoffun, currentfun);
  307.                   }
  308.                   else {
  309.                     displaynumsw = TRUE;
  310.                     displaynums(rp, funs, percent, currentfun, numoffun);
  311.                   }
  312.                   break;
  313.         
  314.                 case 2:               /* Pause*/
  315.                   if (pausesw) {
  316.                     pausesw = FALSE;
  317.                     ModifyIDCMP(w,CLOSEWINDOW|MOUSEBUTTONS|MENUPICK|INTUITICKS);
  318.                     if(adjopen)
  319.                       ModifyIDCMP(adjwin,CLOSEWINDOW|GADGETUP);
  320.                   }
  321.                   else {
  322.                     pausesw = TRUE;   /* set to only look at menupicks/close */
  323.                     ModifyIDCMP(w,CLOSEWINDOW|MENUPICK);
  324.                     if(adjopen)
  325.                       ModifyIDCMP(adjwin,CLOSEWINDOW);
  326.                   }
  327.                   break;
  328.                 case 3:               /* Toggle Bk/Fg  */
  329.                   if (blackbacksw) {
  330.                     blackbacksw = FALSE;
  331.                   }
  332.                   else {
  333.                     blackbacksw = TRUE;
  334.                   }
  335.                   tempcolor   = colortab[0];
  336.                   colortab[0] = colortab[1];
  337.                   colortab[1] = tempcolor;
  338.                   LoadRGB4(ViewPortAddress(w), colortab, 16);
  339.                   break;
  340.               } /* end of inner switch */
  341.             }   /* end of outer switch */
  342.           code = Item->NextSelect;
  343.           }   /* end of if */
  344.           break;  
  345.       } /*  end of outer outer switch */
  346.     } /* end of while */
  347.  
  348. /*  lets see if you want me to do something to adjwin        */
  349.  
  350.   if (adjopen) 
  351.     if((message=(struct IntuiMessage *)GetMsg(adjwin->UserPort))) {
  352.       class = message->Class;
  353.       code  = message->Code;
  354.       aAddress = message->IAddress;
  355.       ReplyMsg(message);      
  356.       switch (class) {
  357.         case  CLOSEWINDOW:        /*  Get rid of the requester     */
  358.           if (adjopen){
  359.             CloseWindow(adjwin);
  360.             adjopen = FALSE;
  361.           }
  362.           break;
  363.         case GADGETUP:       /*reply, then process */
  364.           gadgetmessage(aAddress,adjwin,funs,percent,&xyscale,&xoff,&yoff,
  365.                         gads,PInfos,&numoffun,¤tfun,stxt,
  366.                         displaynumsw,adjopen);
  367.           for (j=0;j<10;j++)           /* settle in on function         */
  368.             graphit(funs, percent, xyscale, xoff, yoff, FALSE); 
  369.           checkswitches(rp,funs,percent,currentfun,
  370.                         numoffun,vectorsw,displaynumsw);
  371.           break;            
  372.       } /* end switch */
  373.     }  /* end if */
  374.   if(!adjdown) 
  375.     graphit(funs, percent, xyscale, xoff, yoff, TRUE); 
  376.   }   /* end FOREVER */   
  377.   return(0);
  378. } /* end main */
  379.  
  380. void clearscreen() {
  381.  
  382.    SetAPen(rp,0);
  383.    RectFill(rp, XSTART, YSTART-HEIGHT, WIDTH-XSTART, YSTART);
  384. }
  385.  
  386. void graphit(funs,percent,xyscale,xoff,yoff,drawit)
  387.  
  388. float funs[][6],percent[],xyscale;
  389. int   xoff,yoff,drawit;
  390.  
  391. {
  392.   double pk;
  393.   int i, ix, iy, p;
  394.   static int color;
  395.   float newx, newy;
  396.  
  397.   p = rand();
  398.                
  399.   pk = p/(float) INT_MAX;
  400.  
  401.   i=0;
  402.  
  403.   while(i<numoffun && pk > percent[i]) {
  404.         i++;
  405.      }
  406.  
  407.   newx = funs[i][0] * x + funs[i][1] * y + funs[i][4];
  408.   newy = funs[i][2] * x + funs[i][3] * y + funs[i][5];
  409.  
  410.   x = newx;
  411.   y = newy;
  412.  
  413.   ix = x*HEIGHT*xyscale+xoff;            /* Assumes that H<W      */
  414.   iy = y*HEIGHT*xyscale+yoff;
  415.  
  416.   if ((color = (i+2)%MAXCOLORS) < 2) 
  417.     color = color+2;
  418.   if (drawit && ix>0 && ix<WIDTH && iy>0 && iy<HEIGHT) {
  419.     SetAPen(rp, color);
  420.     WritePixel(rp, ix, HEIGHT-iy);
  421.   }
  422. }
  423.  
  424. void initialit (funs,percent,xyscale,xoff,yoff,currentfun)
  425.  
  426. float funs[][6],percent[],*xyscale;
  427. int   *xoff,*yoff,*currentfun;
  428.  
  429. {
  430.    int i;
  431.  
  432.    srand(time(&timeint));        /* Set Random Generator Seed    */
  433.                                         /* From Time                    */
  434.    colortab[0] = 0;
  435.    colortab[1] = 4095;
  436.  
  437.    GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
  438.    if (GfxBase == NULL)
  439.       exit(2);
  440.  
  441.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  442.    if (IntuitionBase == NULL) {
  443.       CloseLibrary(GfxBase);
  444.       exit(3);
  445.    }
  446.  
  447.  
  448.    screen = (struct Screen *)OpenScreen(&ns); 
  449.    if (screen == NULL) {
  450.       CloseLibrary(IntuitionBase);
  451.       CloseLibrary(GfxBase);
  452.       exit(4);
  453.       }
  454.  
  455.    nw.Screen = screen;                /* Open window in our new screen */
  456.    w = (struct Window *)OpenWindow(&nw);
  457.    if (w == NULL) {
  458.       CloseScreen(screen);
  459.       CloseLibrary(IntuitionBase);
  460.       CloseLibrary(GfxBase);
  461.       exit(5);
  462.       }
  463.     
  464.    setupgad();
  465.   
  466.    for (i=2;i<FUNLIMIT;i++)  {
  467.       funs[i][0] = 1.;
  468.       funs[i][1] = 0.;
  469.       funs[i][2] = 0.;
  470.       funs[i][3] = 1.;
  471.       funs[i][4] = 0.;
  472.       funs[i][5] = 0.;
  473.       percent[i] = 0.5;
  474.       }
  475.  
  476.    setpots(adjwin, gads, PInfos, funs, percent, *xyscale, *xoff, *yoff,
  477.            *currentfun, numoffun, stxt, adjopen);
  478.  
  479.    SetMenuStrip(w, &Menu[0]);
  480.    ShowTitle(screen, FALSE);           /* No Bars in Arizona           */
  481.  
  482.    vp = &screen->ViewPort;             /* Set colors in screen's VP    */
  483.    rp = w->RPort;                      /* Render into the window's RP  */
  484.    clearscreen();
  485.  
  486. /* check and write indicators */ 
  487.    checkswitches(rp,funs,percent,currentfun,numoffun,vectorsw,displaynumsw);
  488.  
  489. /*  Set the color registers */
  490.    setcolor();
  491.  
  492. /* default color for small flower */
  493.    colortab[2] = 1404;
  494.    colortab[3] = 3913;
  495.    LoadRGB4(ViewPortAddress(w), colortab, 16);
  496. } /* end initialit */
  497.  
  498. void setcolor()  {
  499.  
  500.    int i, colorinc;
  501.  
  502.    colorinc = rand() % 4096;
  503.    
  504.    colortab[2] = rand() % 4096;
  505.  
  506.  
  507.    for (i=3; i<32; i++) 
  508.       colortab[i] = (colortab[i-1] + colorinc) % 4096;
  509.  
  510.    LoadRGB4(ViewPortAddress(w), colortab, 16);
  511.  
  512.    SetBPen(rp, 0);                    /* Insure clean text              */
  513.  
  514. }
  515.  
  516. void setupgad() {
  517.  
  518.   /* This is where the proportional gadgets are set up, using
  519.    * the templates that were declared staticly.
  520.   */
  521.   short i, 
  522.         gadcount;                     /* index to next available Gadget */
  523.  
  524.   for(gadcount = 0; gadcount < NUMPROPS-2; gadcount++) {
  525.     gads[gadcount] = TPropGadget;
  526.     PInfos[gadcount] = TPropInfo;
  527.     gads[gadcount].GadgetText = NULL;       /* no text on prop gads */
  528.     gads[gadcount].GadgetRender = (APTR)
  529.     &PImages[gadcount];
  530.     gads[gadcount].SpecialInfo = (APTR)&PInfos[gadcount];
  531.     gads[gadcount].TopEdge = GADSIZE * (gadcount+2);
  532.     if(gadcount != 0)
  533.     gads[gadcount].NextGadget = &gads[gadcount-1];
  534.   }   /* end for */
  535.  
  536.   gads[NUMPROPS-2] = ZoomGadget;
  537.   PInfos[NUMPROPS-2] = TPropInfo;
  538.   PInfos[NUMPROPS-2].Flags = AUTOKNOB | FREEVERT;
  539.   gads[NUMPROPS-2].SpecialInfo = (APTR)&PInfos[NUMPROPS-2];
  540.   gads[NUMPROPS-2].GadgetRender = (APTR) &PImages[NUMPROPS-2];
  541.   gads[NUMPROPS-2].NextGadget = &gads[NUMPROPS-3];
  542.  
  543.   gads[NUMPROPS-1] = CenterGadget;
  544.   PInfos[NUMPROPS-1] = TPropInfo;
  545.   PInfos[NUMPROPS-1].Flags = AUTOKNOB | FREEVERT | FREEHORIZ;
  546.   gads[NUMPROPS-1].SpecialInfo = (APTR)&PInfos[NUMPROPS-1];
  547.   gads[NUMPROPS-1].GadgetRender = (APTR) &PImages[NUMPROPS-1];
  548.   gads[NUMPROPS-1].NextGadget = &gads[NUMPROPS-2];
  549.   
  550.   PInfos[0].HorizBody = FFFF/(numoffun+1);
  551.  
  552.   /* This is where the String gadgets are set up, using
  553.      the templates that were declared staticly.
  554.   */
  555.  
  556.   for(i=0; i<NUMSTRS; i++) {
  557.     SInfo[i].Buffer     = &stxt[i][0];
  558.     SInfo[i].UndoBuffer = NULL;
  559.     SInfo[i].BufferPos  = 0;
  560.     SInfo[i].MaxChars   = GSTRLEN;
  561.     SInfo[i].DispPos    = 0;
  562.     }
  563.  
  564.   for(gadcount = NUMPROPS; gadcount < NUMGADS; gadcount++) {
  565.     gads[gadcount] = TStrngGadget;
  566.     gads[gadcount].GadgetText = 
  567.       (struct IntuiText *) &StrngText[gadcount-NUMPROPS];
  568.     gads[gadcount].GadgetRender = NULL;
  569.     gads[gadcount].SpecialInfo = (APTR)&SInfo[gadcount-NUMPROPS];
  570.  
  571.     if(gadcount==NUMPROPS)
  572.       gads[gadcount].TopEdge = 20;
  573.     else 
  574.       gads[gadcount].TopEdge = GADSIZE * (gadcount+2-NUMPROPS);
  575.       gads[gadcount].NextGadget = &gads[gadcount-1];
  576.    }   /* end for */
  577. } /* end setupgad */
  578.  
  579. void closeout() {
  580.  
  581.    ClearMenuStrip(w);
  582.    CloseWindow(w);
  583.    CloseScreen(screen);
  584.    CloseLibrary(IntuitionBase);
  585.    CloseLibrary(GfxBase);
  586.  
  587. }
  588.  
  589. void displaynums(rp, funs, percent, currentfun, numoffun)
  590.   struct   RastPort      *rp;
  591.   float funs[][6], percent[];
  592.   int currentfun, numoffun;
  593. {
  594.   short i, j, k, color;
  595.   char str[80];
  596.  
  597.   j = HEIGHT - (8*numoffun) - 10;
  598.   Move(rp,0,j);
  599.    
  600.   for (i=0; i<=numoffun; i++) {
  601.     if ((color = (i+2)%MAXCOLORS) < 2) 
  602.       color = color+2;
  603.     SetAPen(rp, color);
  604.  
  605.     sprintf (str, "%3d ", i+1);
  606.     Text(rp,str,strlen(str));
  607.     for(k=0; k<6; k++) {         
  608.       sprintf (str, "%5.2f ", funs[i][k]);
  609.       Text(rp,str,strlen(str));
  610.     }
  611.     sprintf (str, "%5.2f", ((i>0) ? percent[i]-percent[i-1] : percent[i]));
  612.     Text(rp,str,strlen(str));
  613.     j = j+8;
  614.     Move(rp,0,j);
  615.    }
  616. }
  617.  
  618. void autoadj(funs, percent, xyscale, xoff, yoff, currentfun) 
  619.  
  620. float funs[][6],percent[],*xyscale;
  621. int   *xoff,*yoff,*currentfun;
  622. {
  623. int j;
  624. float minx = 1e20;   
  625. float miny = 1e20;  
  626. float maxx = -minx;
  627. float maxy = -minx;
  628.  
  629. for (j=0;j<100;j++) 
  630.    {graphit(funs, percent, *xyscale, *xoff, *yoff, FALSE); 
  631.     if (x < minx) minx = x;
  632.     if (x > maxx) maxx = x;
  633.     if (y < miny) miny = y;
  634.     if (y > maxy) maxy = y;
  635.     }
  636.  
  637.   if (maxx > minx && maxy > miny && minx > -1e20 && maxx < 1e20
  638.     && miny > -1e20 && maxy < 1e20) {    /* don't scale if either diff = 0 */
  639.  
  640.     if ( (maxx-minx)/WIDTH > (maxy-miny)/HEIGHT) {
  641.       *xyscale = (SCRNSCALE*ASPECT)/ (maxx - minx);
  642.       *xoff = (WIDTH>>1)  * (1 - (SCRNSCALE*(maxx+minx)/(maxx-minx))); 
  643.       *yoff = (HEIGHT>>1) * (1 - (*xyscale * (maxy+miny)));
  644.     }
  645.     else {
  646.       *xyscale = SCRNSCALE / (maxy - miny);
  647.       *xoff = ((WIDTH - HEIGHT * *xyscale * (maxx-minx))/2.0) - (minx * HEIGHT * *xyscale);
  648.       *yoff = (HEIGHT>>1) * (1 - SCRNSCALE - (2.0 * miny * *xyscale)); 
  649.     }
  650.  
  651.     setpots(adjwin, gads, PInfos, funs, percent, *xyscale, *xoff, *yoff,
  652.             *currentfun, numoffun, stxt, adjopen);
  653.  
  654.     }
  655. }    /* end autoadj */
  656.  
  657.  
  658. void getifsfun(fname,numoffun,funs,percent,colortab,xoff,yoff,xyscale)
  659.    char fname[MAXFNLEN];
  660.    int *numoffun;
  661.    float funs[][6], percent[];
  662.    UWORD  colortab[]; 
  663.    int *xoff,*yoff;
  664.    float *xyscale;
  665. {
  666.    FILE *fp;
  667.  
  668.    int tempcolor;
  669.    int i, j;
  670.   
  671.    if ((fp = fopen(fname, "r")) == NULL) {
  672.       printf("ifsout:  can't open %s\n", fname);
  673.    }
  674.    else {
  675.       fscanf(fp, "%d %d %d %f\n", numoffun, xoff, yoff, xyscale);
  676.       for(i=0;i <= *numoffun;i++) {
  677.         for(j=0; j<6; j++)       
  678.           fscanf(fp, "%f ", &funs[i][j]);
  679.         fscanf(fp, "%f %d\n", &percent[i], &tempcolor);
  680.         colortab[i+2] = (UWORD) tempcolor; /* assumes that i<= FUNLIMIT */
  681.       }
  682.       fclose(fp);
  683.       LoadRGB4(ViewPortAddress(w), colortab, 16);
  684.    }
  685. }
  686.  
  687. void putifsfun(fname,numoffun,funs,percent,colortab,xoff,yoff,xyscale)
  688.   char fname[MAXFNLEN];
  689.   int numoffun;
  690.   float funs[][6], percent[];
  691.   UWORD  colortab[]; 
  692.   int xoff,yoff;
  693.   float xyscale;
  694. {
  695.   FILE *fp;
  696.  
  697.   int i,j;
  698.   
  699.   if ((fp = fopen(fname, "w")) == NULL) {
  700.     printf("ifsout:  can't open %s for write\n", fname);
  701.    }
  702.   else {
  703.   fprintf(fp, "%d %d %d %f\n", numoffun, xoff, yoff, xyscale);
  704.   for(i=0;i<=numoffun;i++) {
  705.     for(j=0; j<6; j++)       
  706.       fprintf(fp, "%5.2f ", funs[i][j]);
  707.      /* assumes that FUNLIMIT <= 29 */
  708.     fprintf(fp, " %5.2f %d\n", percent[i], colortab[i+2]);
  709.    }
  710.   fclose(fp);
  711.   }
  712. }
  713.  
  714.  
  715.  
  716.